﻿using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Business.Furnitures;
using blackhouse.WebNemo.Business;
using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace blackhouse.BlackApart.WebNemo {
    public class NemoApartmentType : GroupRecord, IApartmentGroup {

        #region Fields

        private Seo pageSeo;
        private ApartmentOptionField[] fields;
        private Guid guid = Guid.Empty;
        #endregion

        #region Properties

        public int Id {
            get { return this.RecordId; }
        }

        public string Name {
            get { return this.GetString("nazwa"); }
            set { this.SetValue("nazwa", value); }
        }

        public string ShortName {
            get { return this.GetString("krotka_nazwa"); }
            set { this.SetValue("krotka_nazwa", value); }
        }

        public int NumberOfPeople {
            get { return this.GetInt32("liczba_osob"); }
            set { this.SetValue("liczba_osob", value); }
        }

        public IApartmentLocation DefaultLocalization {
            get { return this.GetRecord<NemoLocation>("domyslna_lokalizacja"); }
            set { throw new NotImplementedException(); }
        }

        public float AreaSize {
            get { return this.GetSingle("rozmiar"); }
            set { throw new NotImplementedException(); }
        }

        public double Popularity {
            get {
                return 0;
            }
            set {
                throw new NotImplementedException();
            }
        }

        public float Stars {
            get { return this.GetSingle("ilosc_gwiazdek"); }
            set { throw new NotImplementedException(); }
        }

        public IApartmentPhoto MainPhoto {
            get { return this.GetRecord<NemoApartmentPhoto>("glowne_zdjecie"); }
            set { throw new NotImplementedException(); }
        }

        public DateTime LastMod {
            get { return this.LastModifiedTime; }
            set { throw new NotImplementedException(); }
        }

        public ApartmentOptionField[] Fields {
            get {
                if (this.fields == null)
                    this.fields = this.GetFields();
                return this.fields;
            }
            set { throw new NotImplementedException(); }
        }

        public string LidDescription {
            get { return this.GetString("lid"); }
        }

        public string Description {
            get { return this.GetString("opis"); }
        }

        public string Url {
            get { return this.GetString("page_url"); }
            set { this.SetValue("page_url", value); }
        }

        public int UrlId {
            get {
                return this.IsNotNull("page_urlid") ? this.GetInt32("page_urlid") : 0;
            }
            set { this.SetValue("page_urlid", value); }
        }

        public Guid Guid {
            get {
                if (this.guid.Equals(Guid.Empty))
                    Guid.TryParse(this.GetString("guid"), out this.guid);
                return this.guid;
            }
            set { this.SetValue("guid", value.ToString()); }
        }

        #endregion

        #region Constructors

        public NemoApartmentType(IGroup group, IGroupRecord record) : base(group, record) { }

        public NemoApartmentType()
            : this(0) {
        }

        public NemoApartmentType(int apartmentTypeId)
            : base("TYP_APARTAMENTU", apartmentTypeId) {

        }

        #endregion

        #region Methods

        private ApartmentOptionField[] GetFields() {
            var fieldRecords = this.GetRecords("topfields");
            var topfields = (
                from fieldRecord in fieldRecords
                let title = fieldRecord.GetString("tytul")
                let description = fieldRecord.GetString("dodatkowy_opis")
                let variant = fieldRecord.GetInt32("wariant")
                select new ApartmentOptionField(title, variant, description)
                ).ToList();
            return topfields.OrderBy(f => f.Title).ToArray();
        }

        public new bool Save() {
            this.Save(false);
            return true;
        }

        public bool Delete() {
            throw new NotImplementedException();
        }

        public new bool Exists() {
            return base.Exists;
        }

        public IApartment MakeDefaultApartment() {
            throw new NotImplementedException();
        }

        public IApartment[] GetApartments() {
            var group = Factory.Instance().GetGroup("apartamenty");
            var q = new GroupRecordQuery(group);
            q.AddWhereClause("typ_apartamentu", this.Id);
            var gr = new GroupRecords(group);
            // ReSharper disable once CoVariantArrayConversion
            return gr.GetRecords<NemoApartment>(q);
        }

        public IApartmentGroup[] GetAllApartmentTypes() {
            return GetAll(null);
        }

        public static IApartmentGroup[] GetAll(CultureInfo language) {
            var apartmentTypes =
                (language == null) ?
                new GroupRecords("TYP_APARTAMENTU") :
                new GroupRecords("TYP_APARTAMENTU", language);
            // ReSharper disable once CoVariantArrayConversion
            return apartmentTypes.GetRecords<NemoApartmentType>().ToArray();
        }

        public static IGroup GetNemoGroup() {
            return Factory.Instance().GetGroup("TYP_APARTAMENTU");
        }

        public object GetProperty(string propertyKey) {
            throw new NotImplementedException();
        }

        public Dictionary<string, string> GetArrayProperty(string propertyKey) {
            throw new NotImplementedException();
        }

        public Seo GetSeo() {
            if (this.pageSeo == null) {
                this.pageSeo = new Seo
                {
                    PageTitle = this.GetString("page_title"),
                    PageKeywords = this.GetString("page_keywords"),
                    PageDescription = this.GetString("page_description")
                };
            }
            return this.pageSeo;
        }

        public PageSeo GetPageSeo() {
            string title = String.Empty, description = String.Empty;
            if (this.IsNotNull("page_title"))
                title = this.GetString("page_title");
            if (String.IsNullOrEmpty(title))
                title = this.Name;
            if (this.IsNotNull("page_description"))
                description = this.GetString("page_description");
            if (String.IsNullOrEmpty(description))
                description = this.LidDescription;
            var url = this.Url;
            return new PageSeo(title, description, url);
        }

        public IApartmentPhoto[] GetAllPhotos() {
            var apartmentPhotos = new List<IApartmentPhoto>(this.GetRecords<NemoApartmentPhoto>("pozostale_zdjecia").ToArray());
            var gr = new GroupRecord("SYS_GALERIA_HOTELU", 1);
            if (gr.Exists)
            {
// ReSharper disable once CoVariantArrayConversion
                IApartmentPhoto[] hotelPhotos = gr.GetRecords<NemoApartmentPhoto>("zdjecia");
                apartmentPhotos.AddRange(hotelPhotos);
            }
            return apartmentPhotos.ToArray();
        }

        public IFurniture[] GetFurnitures() {
            GroupRecord sharedType = this.GetRecord("wspoldzielony_typ");
// ReSharper disable once CoVariantArrayConversion
            return sharedType != null ? sharedType.GetRecords<NemoFurniture>("wyposazenie").ToArray() : new IFurniture[0];
        }

        #endregion

    }
}
